package com.cax.module.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cax.common.constant.Constants;
import com.cax.common.domain.base.dto.IdIntegerDTO;
import com.cax.common.utils.NumberUtils;
import com.cax.common.utils.PageUtils;
import com.cax.module.article.domain.dto.ArticlePageDTO;
import com.cax.module.article.mapper.ArticleMapper;
import com.cax.module.article.service.ArticleService;
import com.cax.module.file.domain.FileType;
import com.cax.module.file.domain.vo.File2VO;
import com.cax.module.file.service.FileService;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.cax.module.article.domain.vo.ArticleVO;
import com.cax.module.article.domain.dto.ArticleDTO;
import com.cax.module.article.domain.Article;
import cn.hutool.core.bean.BeanUtil;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 文章表 Service实现类
 * @author makejava
 * @date 2024-08-22 17:22:38
 */
@Service
@Slf4j
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

	@Autowired
	ArticleMapper articleMapper;
	@Autowired
	FileService fileService;

   /**
	 * 查询所有
	 */
	@Override
	public List<ArticleVO> getAll(){
		List<Article> list = this.list();
		List<ArticleVO> result = BeanUtil.copyToList(list, ArticleVO.class);
		return result;
	}

	/**
     * 分页查询
     */
	@Override
    public Page<ArticleVO> getPage(ArticlePageDTO dto){
		Page<Article> page = dto.createPage(dto);
		LambdaQueryWrapper<Article> lqw = new LambdaQueryWrapper<>();
		if (!NumberUtils.integerEmpty(dto.getArticleColumnId())) {
			lqw.eq(Article::getArticleColumnId, dto.getArticleColumnId());
		}
		if (StringUtils.isNotEmpty(dto.getSearchText())) {
			lqw.like(true, Article::getTitle,dto.getSearchText());
		}
		if (!Objects.isNull(dto.getIsView())) {
			lqw.eq(Article::getIsView,dto.getIsView());
		}
		if (!Objects.isNull(dto.getIsRoll())) {
			lqw.eq(Article::getIsRoll,dto.getIsRoll());
		}
		if (!Objects.isNull(dto.getIsTop())) {
			lqw.eq(Article::getIsTop,dto.getIsTop());
		}
		lqw.eq(Article::getDeleteFlag, Constants.NO);
		lqw.orderByDesc(Article::getUpdateTime);
		this.page(page, lqw);
		if(CollectionUtils.isEmpty(page.getRecords())){
			return PageUtils.empty(page);
		}
		Page<ArticleVO> result = PageUtils.entiryTypeConvert(page, ArticleVO.class);

		//添加文件
		List<ArticleVO> records = result.getRecords();
		List<Integer> ids = records.stream()
				.map(i -> i.getId())
				.collect(Collectors.toList());
		Map<String,List<File2VO>> fileMap = fileService.getMap(
				Lists.newArrayList(FileType.TypeEnum.FILE_TYPE_1.getCode(), FileType.TypeEnum.FILE_TYPE_2.getCode()),
				new ArrayList<>(ids)//隐式转换为List<Object>
		);
		for(ArticleVO i:records){
			String titleKey = FileType.TypeEnum.FILE_TYPE_1.getCode()+"-"+i.getId();
			String richKey = FileType.TypeEnum.FILE_TYPE_2.getCode()+"-"+i.getId();
			if(fileMap.containsKey(titleKey)){
				i.setTitleFiles(fileMap.get(titleKey));
			}
			if(fileMap.containsKey(richKey)){
				i.setRichFiles(fileMap.get(richKey));
			}
		}

		return result;
	}

	/**
	 * 通过主键查询
	 */
	@Override
	public ArticleVO getByKey(Integer id){
		Article entity = super.getById(id);
		ArticleVO result = BeanUtil.copyProperties(entity, ArticleVO.class);

		List<File2VO> titleFiles = fileService.get(FileType.TypeEnum.FILE_TYPE_1.getCode(), result.getId());
		result.setTitleFiles(titleFiles);

		List<File2VO> richFiles = fileService.get(FileType.TypeEnum.FILE_TYPE_2.getCode(), result.getId());
		result.setRichFiles(richFiles);

        return result;
    }

	/**
	 * 添加
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Integer add(ArticleDTO dto){
		Article article = BeanUtil.copyProperties(dto, Article.class);
		this.save(article);

		List<Long> fileIds = new ArrayList<>();
		if(!CollectionUtils.isEmpty(dto.getTitleFileIds())){
			fileIds.addAll(dto.getTitleFileIds());
		}
		if(!CollectionUtils.isEmpty(dto.getRichFileIds())){
			fileIds.addAll(dto.getRichFileIds());
		}
		if(!CollectionUtils.isEmpty(fileIds)){
			fileService.addSave(article.getId(),fileIds);
		}

		return article.getId();
	}

	/**
	 * 通过主键修改
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void upd(ArticleDTO dto){
		Article article = BeanUtil.copyProperties(dto, Article.class);
		article.setUpdateTime(LocalDateTime.now());
	    this.updateById(article);

		fileService.updSave(FileType.TypeEnum.FILE_TYPE_1.getCode(),dto.getId(),dto.getTitleFileIds());
		fileService.updSave(FileType.TypeEnum.FILE_TYPE_2.getCode(),dto.getId(),dto.getRichFileIds());
	}

	@Override
	public void del(IdIntegerDTO dto){
		Article article = new Article();
		article.setId(dto.getId());
		article.setDeleteFlag(Constants.YES);
		this.updateById(article);
	}
}



